#-*- mode: python -*-
def _popen(cmd_arg):
    from subprocess import Popen, PIPE
    devnull = open('/dev/null')
    cmd = Popen(cmd_arg, stdout=PIPE, stderr=devnull, shell=True)
    retval = cmd.stdout.read().strip()
    err = cmd.wait()
    cmd.stdout.close()
    devnull.close()
    if err:
        raise RuntimeError("Failed to close %s stream" % cmd_arg)
    return retval

def environ_append(key, value, separator=' '):
    old_value = os.environ.get(key)
    if old_value is not None:
        value = old_value + separator + value
    os.environ[key] = value

def append_autogenargs(module, args):
    old_value = module_autogenargs.get(module, autogenargs)
    module_autogenargs[module] = old_value + " " + args

def remove_autogenargs(module, args):
    arg_string = module_autogenargs.get(module, autogenargs)
    module_autogenargs[module] = arg_string.replace(args, "")
def add_to_path_var(var, path):
    if not var in os.environ:
        os.environ[var] = path
    else:
        os.environ[var] = os.environ[var] + ":" + path

_basedir = "@-BASE_DIR-@"
_download_dir = "@-DOWNLOAD_DIR-@"
_arch = "@-ARCH-@"
if 'TARGET' in os.environ:
    (_project, _branch) = os.environ['TARGET'].split('-')
else:
    _project = 'gnucash'
    _branch = 'master'
_projectdir = os.path.join(_basedir, _project, _branch)
_config_dir = os.path.dirname(os.path.realpath(__file__))
prefix = os.path.join(_projectdir, 'inst')
checkoutroot = os.path.join(_projectdir, 'src')
buildroot = os.path.join(_projectdir, 'build')
tarballdir = os.path.join(_download_dir)
moduleset = os.path.join(_config_dir, 'gnucash.modules')
_mingw_base = os.path.join(_basedir, 'msys2', _arch)
_mingw_includes = os.path.join(_mingw_base, 'include')
_mingw_libs = os.path.join(_mingw_base, 'lib')
_usr_include = os.path.join('/usr', 'include')
_usr_lib = os.path.join('/usr', 'lib')
environ_append('CPPFLAGS', '-I%s -I%s' % (_mingw_includes, _usr_include))
environ_append('LDFLAGS', '-L%s -L%s' % (_mingw_libs, _usr_lib))
add_to_path_var('PKG_CONFIG_PATH', os.path.join(_mingw_libs, 'pkgconfig'))
add_to_path_var('PATH', os.path.join(_mingw_base, 'bin'))

os.environ['SRCROOT'] = checkoutroot
os.environ['PREFIX'] = prefix
os.environ['MSYS2ROOT'] = os.path.join(_basedir, 'msys2')
#os.environ['GUILE_LOAD_PATH'] = os.path.join(prefix, 'share', 'guile', '2.0')
#os.environ['GUILE_LOAD_BINARY_PATH'] = os.path.join(prefix, 'lib', 'guile', '2.0', 'ccache')
os.environ['GETTEXTDATADIR'] = os.path.join(_mingw_base, 'share', 'gettext')

if _arch == 'mingw32':
    autogenargs = "--build=i686-w64-mingw32"
else:
    autogenargs = "--build=x86_64-w64-mingw64"

#autogenargs = 'CPPFLAGS="-I%s"' % _sys_includes

if _branch == 'releases':
    modules = ["meta-gnucash"]
elif _branch == 'unstable':
    modules = ['meta-gnucash-unstable']
else:
    modules = ["meta-gnucash-git"]
    branches['gnucash-git'] = (None, _branch)
    branches['gnucash-docs-git'] = (None, _branch)

append_autogenargs("libofx", "--with-opensp-includes=" + prefix + "/include/OpenSP --with-opensp-libs=" + prefix + "/lib")
append_autogenargs("libdbi-drivers","--with-dbi-incdir=" + prefix + "/include --with-dbi-libdir=" + prefix + "/lib")
module_makeargs["aqbanking"]="-j 1"

module_extra_env["gnucash"]={'GUILE_LOAD_PATH':os.path.join(prefix, 'share', 'guile', '2.2'), 'GUILE_LOAD_COMPILED_PATH':os.path.join(prefix, 'lib', 'guile', '2.2', 'ccache')}
module_extra_env["gnucash-unstable"]={'GUILE_LOAD_PATH':os.path.join(prefix, 'share', 'guile', '2.2'), 'GUILE_LOAD_COMPILED_PATH':os.path.join(prefix, 'lib', 'guile', '2.2', 'ccache')}
module_extra_env["gnucash-git"]={'GUILE_LOAD_PATH':os.path.join(prefix, 'share', 'guile', '2.2'), 'GUILE_LOAD_COMPILED_PATH':os.path.join(prefix, 'lib', 'guile', '2.2', 'ccache')}

_bdw_gc_cflags = '-fcommon'
if 'CFLAGS' in os.environ:
    _bdw_gc_cflags = os.environ['CFLAGS'] + ' -fcommon'

module_extra_env['bdw-gc']={'CFLAGS':_bdw_gc_cflags}

